package com.spring.sunflower.conversation;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseApplication;
import com.spring.sunflower.bean.MessageEntity;
import com.spring.sunflower.util.DisplayUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * Created by Kevin on 2021/6/5<br/>
 * <p>
 * Describe:<br/>
 */
public class ChatAdapter extends BaseMultiItemQuickAdapter<MessageEntity, BaseViewHolder> {
    private static final int AUDIO_MIN_WIDTH = DisplayUtils.dip2px(BaseApplication.getContext(), 60);
    private static final int AUDIO_MAX_WIDTH = DisplayUtils.dip2px(BaseApplication.getContext(), 250);

    public ChatAdapter(@Nullable List<MessageEntity> data) {
        super(data);
        addItemType(MessageEntity.MSG_TYPE_TEXT, R.layout.adapter_conversation_message_text);
        addItemType(MessageEntity.MSG_TYPE_IMAGE, R.layout.adapter_conversation_message_image);
        addItemType(MessageEntity.MSG_TYPE_AUDIO, R.layout.adapter_conversation_message_audio);
        addItemType(MessageEntity.MSG_TYPE_VIDEO, R.layout.adapter_conversation_message_video);
        addItemType(MessageEntity.MSG_TYPE_CUSTOM, R.layout.adapter_conversation_message_video);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, MessageEntity messageEntity) {
//        switch (holder.getItemViewType()) {
//            case MessageEntity.MSG_TYPE_TEXT:
//                processCommon(holder, messageEntity);
//                TextView tvText = holder.getView(R.id.tv_text);
//                holder.setText(R.id.tv_text, SpanStringUtils.getEmotionContent(EMOTION_CLASSIC_TYPE, getContext(), tvText, messageEntity.getTextElem().getText()));
//
//                break;
//            case MessageEntity.MSG_TYPE_IMAGE:
//                boolean self = messageEntity.isSelf();
//                ImageView userIconLeft = holder.getView(R.id.user_icon_left);
////                LinearLayout llContent = holder.getView(R.id.llContent);
//                ImageView userIconRight = holder.getView(R.id.user_icon_right);
//                if (self) {
//                    userIconLeft.setVisibility(View.GONE);
//                    userIconRight.setVisibility(View.VISIBLE);
//                    Glide.with(getContext())
//                            .load(messageEntity.getFaceUrl())
//                            .error(R.drawable.ic_placeholder)
//                            .into(userIconRight);
//                } else {
//                    userIconLeft.setVisibility(View.VISIBLE);
//                    userIconRight.setVisibility(View.GONE);
//                    Glide.with(getContext())
//                            .load(messageEntity.getFaceUrl())
//                            .error(R.drawable.ic_placeholder)
//                            .into(userIconLeft);
//                }
//                RoundedImageView imageView = holder.getView(R.id.image);
//                List<V2TIMImageElem.V2TIMImage> imageList = messageEntity.getImageElem().getImageList();
//                for (V2TIMImageElem.V2TIMImage v2TIMImage : imageList) {
//                    String uuid = v2TIMImage.getUUID(); // 图片 ID
//                    int imageType = v2TIMImage.getType(); // 图片类型
//                    int size = v2TIMImage.getSize(); // 图片大小（字节）
//                    int width = v2TIMImage.getWidth(); // 图片宽度
//                    int height = v2TIMImage.getHeight(); // 图片高度
//                    // 设置图片下载路径 imagePath，这里可以用 uuid 作为标识，避免重复下载
//                    String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
//                    LogUtils.dTag("ChatAdapter", "absolutePath=" + absolutePath);
//                    String imagePath = absolutePath + File.separator + "myUserID" + uuid;
//                    File imageFile = new File(imagePath);
//                    if (imageType == V2TIMImageElem.V2TIM_IMAGE_TYPE_THUMB) {
//                        if (!imageFile.exists()) {
//                            v2TIMImage.downloadImage(imagePath, new V2TIMDownloadCallback() {
//                                @Override
//                                public void onProgress(V2TIMElem.V2ProgressInfo v2ProgressInfo) {
//
//                                }
//
//                                @Override
//                                public void onError(int i, String s) {
//
//                                }
//
//                                @Override
//                                public void onSuccess() {
//                                    Glide.with(getContext())
//                                            .load(imagePath)
//                                            .error(R.drawable.ic_placeholder)
//                                            .into(imageView);
//                                }
//                            });
//                        } else {
//                            Glide.with(getContext())
//                                    .load(imagePath)
//                                    .error(R.drawable.ic_placeholder)
//                                    .into(imageView);
//                        }
//                    }
//                }
//
//                break;
//            case MessageEntity.MSG_TYPE_AUDIO:
//                processCommon(holder, messageEntity);
//                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
//                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//                params.addRule(RelativeLayout.CENTER_VERTICAL);
//                ImageView audioPlayImage = holder.getView(R.id.iv_audio_play);
//                LinearLayout audioContentView = holder.getView(R.id.llContent);
//                FrameLayout msgContentFrame = holder.getView(R.id.fl_msg_content);
//                TextView audioTimeText = holder.getView(R.id.tv_audio_time);
//                if (messageEntity.isSelf()) {
//                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//                    params.rightMargin = 24;
//                    audioPlayImage.setImageResource(R.drawable.msg_voice_playing_3);
//                    audioPlayImage.setRotation(180f);
//                    audioContentView.removeView(audioPlayImage);
//                    audioContentView.addView(audioPlayImage);
//                } else {
//                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//                    params.leftMargin = 24;
//                    // TODO 图标不对
//                    audioPlayImage.setImageResource(R.drawable.msg_voice_playing_3);
//                    audioContentView.removeView(audioPlayImage);
//                    audioContentView.addView(audioPlayImage, 0);
////                    if (msg.getCustomInt() == UNREAD) {
////                        LinearLayout.LayoutParams unreadParams = (LinearLayout.LayoutParams) isReadText.getLayoutParams();
////                        unreadParams.gravity = Gravity.CENTER_VERTICAL;
////                        unreadParams.leftMargin = 10;
////                        unreadAudioText.setVisibility(View.VISIBLE);
////                        unreadAudioText.setLayoutParams(unreadParams);
////                    } else {
////                        unreadAudioText.setVisibility(View.GONE);
////                    }
//                }
////                audioContentView.setLayoutParams(params);
//                // 语音消息
//                V2TIMSoundElem v2TIMSoundElem = messageEntity.getSoundElem();
//                // 语音 ID,内部标识，可用于外部缓存 key
//                String uuid = v2TIMSoundElem.getUUID();
//                // 语音文件大小
//                int dataSize = v2TIMSoundElem.getDataSize();
//                // 语音时长
//                int duration = v2TIMSoundElem.getDuration();
//                if (duration == 0) {
//                    duration = 1;
//                }
//                // 设置语音文件路径 soundPath，这里可以用 uuid 作为标识，避免重复下载
//                String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
//                String soundPath = absolutePath + File.separator + "myUserID" + uuid;
//                File soundFile = new File(soundPath);
//                // 判断 soundPath 下有没有已经下载过的语音文件
//                if (!soundFile.exists()) {
//                    v2TIMSoundElem.downloadSound(soundPath, new V2TIMDownloadCallback() {
//                        @Override
//                        public void onProgress(V2TIMElem.V2ProgressInfo progressInfo) {
//                            // 下载进度回调：已下载大小 v2ProgressInfo.getCurrentSize()；总文件大小 v2ProgressInfo.getTotalSize()
//                        }
//
//                        @Override
//                        public void onError(int code, String desc) {
//                            // 下载失败
//                        }
//
//                        @Override
//                        public void onSuccess() {
//                            // 下载完成
//                        }
//                    });
//                } else {
//                    // 文件已存在
//                }
//                ViewGroup.LayoutParams audioParams = msgContentFrame.getLayoutParams();
//                audioParams.width = AUDIO_MIN_WIDTH + DisplayUtils.dip2px(getContext(), duration * 6);
//                if (audioParams.width > AUDIO_MAX_WIDTH) {
//                    audioParams.width = AUDIO_MAX_WIDTH;
//                }
//                msgContentFrame.setLayoutParams(audioParams);
//                audioTimeText.setText(duration + "''");
//                msgContentFrame.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        if (AudioPlayer.getInstance().isPlaying()) {
//                            AudioPlayer.getInstance().stopPlay();
//                            return;
//                        }
////                        if (TextUtils.isEmpty(msg.getDataPath())) {
////                            ToastUtil.toastLongMessage(TUIKit.getAppContext().getString(R.string.voice_play_tip));
////                            return;
////                        }
//                        audioPlayImage.setImageResource(R.drawable.play_voice_message);
//                        if (messageEntity.isSelf()) {
//                            audioPlayImage.setRotation(180f);
//                        }
//                        final AnimationDrawable animationDrawable = (AnimationDrawable) audioPlayImage.getDrawable();
//                        animationDrawable.start();
////                        msg.setCustomInt(READ);
////                        unreadAudioText.setVisibility(View.GONE);
//                        AudioPlayer.getInstance().startPlay(soundPath, new AudioPlayer.Callback() {
//                            @Override
//                            public void onCompletion(Boolean success) {
//                                audioPlayImage.post(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        animationDrawable.stop();
//                                        audioPlayImage.setImageResource(R.drawable.msg_voice_playing_3);
//                                        if (messageEntity.isSelf()) {
//                                            audioPlayImage.setRotation(180f);
//                                        }
//                                    }
//                                });
//                            }
//                        });
//                    }
//                });
//                break;
//            case MessageEntity.MSG_TYPE_VIDEO:
//                processVideo(holder, messageEntity);
//                break;
//            default:
//                break;
//        }
//    }
//
//    private void processVideo(@NotNull BaseViewHolder holder, MessageEntity messageEntity) {
//        boolean self = messageEntity.isSelf();
//        ImageView userIconLeft = holder.getView(R.id.user_icon_left);
//        LinearLayout llContent = holder.getView(R.id.llContent);
//        ImageView userIconRight = holder.getView(R.id.user_icon_right);
//        if (self) {
//            userIconLeft.setVisibility(View.GONE);
//            userIconRight.setVisibility(View.VISIBLE);
//            Glide.with(getContext())
//                    .load(messageEntity.getFaceUrl())
//                    .error(R.drawable.ic_placeholder)
//                    .into(userIconRight);
//        } else {
//            userIconLeft.setVisibility(View.VISIBLE);
//            userIconRight.setVisibility(View.GONE);
//            Glide.with(getContext())
//                    .load(messageEntity.getFaceUrl())
//                    .error(R.drawable.ic_placeholder)
//                    .into(userIconLeft);
//        }
//        RoundedImageView imageView = holder.getView(R.id.image);
//        // 视频消息
//        V2TIMVideoElem v2TIMVideoElem = messageEntity.getVideoElem();
//        // 视频截图 ID,内部标识，可用于外部缓存 key
//        String snapshotUUID = v2TIMVideoElem.getSnapshotUUID();
//        // 视频截图文件大小
//        int snapshotSize = v2TIMVideoElem.getSnapshotSize();
//        // 视频截图宽
//        int snapshotWidth = v2TIMVideoElem.getSnapshotWidth();
//        // 视频截图高
//        int snapshotHeight = v2TIMVideoElem.getSnapshotHeight();
//        // 视频 ID,内部标识，可用于外部缓存 key
//        String videoUUID = v2TIMVideoElem.getVideoUUID();
//        // 视频文件大小
//        int videoSize = v2TIMVideoElem.getVideoSize();
//        // 视频时长
//        int duration = v2TIMVideoElem.getDuration();
//        // 设置视频截图文件路径，这里可以用 uuid 作为标识，避免重复下载
//        String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
////        String snapshotPath = absolutePath + File.separator + "snapshot/" + "myUserID" + snapshotUUID;
//        String snapshotPath = absolutePath + File.separator + "myUserID" + snapshotUUID;
//        LogUtils.dTag("ChatAdapter", snapshotPath);
//        File snapshotFile = new File(snapshotPath);
//        if (!snapshotFile.exists()) {
//            v2TIMVideoElem.downloadSnapshot(snapshotPath, new V2TIMDownloadCallback() {
//                @Override
//                public void onProgress(V2TIMElem.V2ProgressInfo progressInfo) {
//                    // 下载进度回调：已下载大小 v2ProgressInfo.getCurrentSize()；总文件大小 v2ProgressInfo.getTotalSize()
//                }
//
//                @Override
//                public void onError(int code, String desc) {
//                    // 下载失败
//                    LogUtils.dTag("ChatAdapter", "code=" + code + ",desc=" + desc);
//                }
//
//                @Override
//                public void onSuccess() {
//                    // 下载完成
//                    Glide.with(getContext())
//                            .load(snapshotPath)
//                            .error(R.drawable.ic_placeholder)
//                            .into(imageView);
//                }
//            });
//        } else {
//            // 文件已存在
//            Glide.with(getContext())
//                    .load(snapshotPath)
//                    .error(R.drawable.ic_placeholder)
//                    .into(imageView);
//        }
//
//        // 设置视频文件路径，这里可以用 uuid 作为标识，避免重复下载
//        String videoPath = absolutePath + File.separator + "video/" + "myUserID" + snapshotUUID;
//        File videoFile = new File(videoPath);
////        if (!snapshotFile.exists()) {
////            v2TIMVideoElem.downloadSnapshot(videoPath, new V2TIMDownloadCallback() {
////                @Override
////                public void onProgress(V2TIMElem.V2ProgressInfo progressInfo) {
////                    // 下载进度回调：已下载大小 v2ProgressInfo.getCurrentSize()；总文件大小 v2ProgressInfo.getTotalSize()
////                }
////
////                @Override
////                public void onError(int code, String desc) {
////                    // 下载失败
////                }
////
////                @Override
////                public void onSuccess() {
////                    // 下载完成
////
////                }
////            });
////        } else {
////            // 文件已存在
////        }
//    }
//
//    private void processCommon(@NotNull BaseViewHolder holder, MessageEntity messageEntity) {
//        boolean self = messageEntity.isSelf();
//        ImageView userIconLeft = holder.getView(R.id.user_icon_left);
//        LinearLayout llContent = holder.getView(R.id.llContent);
//        ImageView userIconRight = holder.getView(R.id.user_icon_right);
//        if (self) {
//            userIconLeft.setVisibility(View.GONE);
//            userIconRight.setVisibility(View.VISIBLE);
//            Glide.with(getContext())
//                    .load(messageEntity.getFaceUrl())
//                    .error(R.drawable.ic_placeholder)
//                    .into(userIconRight);
//            llContent.setBackgroundResource(R.drawable.chat_bubble_myself);
//        } else {
//            userIconLeft.setVisibility(View.VISIBLE);
//            userIconRight.setVisibility(View.GONE);
//            Glide.with(getContext())
//                    .load(messageEntity.getFaceUrl())
//                    .error(R.drawable.ic_placeholder)
//                    .into(userIconLeft);
//            llContent.setBackgroundResource(R.drawable.chat_bubble_other);
//        }
    }
}
